package net.ant.watercalc.web.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.io.IoUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;
import net.ant.watercalc.web.pojo.bo.Report;

import java.io.*;
import java.net.MalformedURLException;

public class PdfUtil {

    public static Image getLogoTop(Document document) throws IOException, BadElementException {
        InputStream input =  PdfUtil.class.getClassLoader().getResourceAsStream("static/img/logo1.jpg");
        byte[] logoArr = IoUtil.readBytes(input);
        Image logoImg = Image.getInstance(logoArr);

        float height = logoImg.getHeight();
        float width  = logoImg.getWidth();
        float percent = getPercent2(height,width,50);
        logoImg.setAlignment(Image.LEFT);
        logoImg.setAlignment(Image.TEXTWRAP);
        logoImg.scalePercent(percent);
        logoImg.setAbsolutePosition(18,770);

        return logoImg;
    }

    public static Image getLogo(Document doc) throws IOException, BadElementException {
        InputStream input =  PdfUtil.class.getClassLoader().getResourceAsStream("static/img/logo.jpeg");
        byte[] logoArr = IoUtil.readBytes(input);
        Image logoImg = Image.getInstance(logoArr);

        float height = logoImg.getHeight();
        float width  = logoImg.getWidth();
        float percent = getPercent2(height,width,100);
        logoImg.setAlignment(Image.LEFT);
        logoImg.setAlignment(Image.TEXTWRAP);
        logoImg.scalePercent(percent);
        logoImg.setAbsolutePosition(5,200);

        return logoImg;
    }

    public static Image getChart(String img, Document doc) throws IOException, DocumentException {
        Image chart1 = Image.getInstance(ImgUtil.getImageBytes(img));

        //Image chart1 = Image.getInstance(ImgUtil.getImageBySvg(img));
        float height = chart1.getHeight();
        float width = chart1.getWidth();
        float percent = getPercent2(height, width,500);
        chart1.setAlignment(Image.MIDDLE);
        chart1.setAlignment(Image.TEXTWRAP);
        chart1.scalePercent(percent);
        chart1.setAbsolutePosition(50,600);

        return chart1;
    }

    private static float getPercent2(float h, float w,int rate) {
        int p = 0;
        float p2 = 0.0f;
        p2 = rate / w * 100;
        //p = Math.round(p2);
        return p2;
    }

}
